package com.ruoyi.api.job;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CrmUser;
import com.ruoyi.system.domain.CrmJob;
import com.ruoyi.system.domain.SysUserNotice;
import com.ruoyi.system.service.ICrmUserService;
import com.ruoyi.system.service.ICrmJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/crmJob")
public class CrmJobController  extends BaseController {

    @Autowired
    private ICrmJobService crmJobService;
    @Autowired
    private ICrmUserService crmCustomerService;

    /**
     * 查看事件列表
     * @param crmJob
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(CrmJob crmJob){
        AjaxResult ajax = AjaxResult.success();
        startPage();
        List<CrmJob> list = crmJobService.selectCrmJobList(crmJob);
        ajax.put("data",getDataTable(list));

        return ajax;
    }


    /**
     * 添加事件
     * @param crmJob
     * @return
     */
    @PostMapping("/insertCrmJob")
    public AjaxResult insertCrmJob(@RequestBody CrmJob crmJob){
        if(StringUtils.isEmpty(crmJob.getJobName())){
            return AjaxResult.error("请填写任务名");
        }
        if(StringUtils.isEmpty(crmJob.getJobLalg())){
            return AjaxResult.error("未填写客户坐标");
        }
        return crmJobService.insertCrmJob(crmJob) > 0 ? AjaxResult.success("添加成功"): AjaxResult.error("添加失败");
    }

    /**
     * 编辑事件
     * @param crmJob
     * @return
     */
    @PostMapping("/updateCrmJob")
    public AjaxResult updateCrmJob(@RequestBody CrmJob crmJob){
        if(StringUtils.isEmpty(crmJob.getJobName())){
            return AjaxResult.error("请填写任务名");
        }
        if(StringUtils.isEmpty(crmJob.getJobLalg())){
            return AjaxResult.error("未填写客户坐标");
        }
        return crmJobService.updateCrmJob(crmJob) > 0 ? AjaxResult.success("编辑成功"): AjaxResult.error("编辑失败");
    }

    /**
     * 根据时间 按天查看事件列表
     * @param date 2020-01-02
     * @return
     */
    @GetMapping("/listByDate")
    public AjaxResult listByDate(String date){
        AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        try {
            Date dateinfo = simpleDateFormat.parse(date);
            startPage();
            List<CrmJob> list = crmJobService.selectCrmJobByDay( dateinfo, userId);
             ajax.put("data",getDataTable(list));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ajax;
    }

    /**
     * 今日待办
     * @param  '2020-01-02'
     * @return
     */
    @GetMapping("/listByToday")
    public AjaxResult listByToday(
            @RequestParam(value="jobName",required=false) String  jobName

            ){
        AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        startPage();
        List<CrmJob> list = crmJobService.selectCrmJobByToday(jobName,userId);
        ajax.put("data",getDataTable(list));
        return ajax;
    }
    /**
     * 明天及以后待办 时间正序 一页10条 取更多加载
     * pageNum
     * pageSize
     * @return
     */
    @GetMapping("/listByNextDate")
    public AjaxResult listByNextDate(
            @RequestParam(value="jobName",required=false) String  jobName,
            @RequestParam(value="pageNum",required=false,defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize
    ){
        AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        startPage();
        List<CrmJob> list = crmJobService.selectCrmJobByNextDay(jobName,userId);
        ajax.put("data",getDataTable(list));
        return ajax;
    }
    /**
     * 今天以前的 时间倒序 一页10条 取更多加载
     * @return
     */
    @GetMapping("/listByPreDate")
    public AjaxResult listByPreDate(
            @RequestParam(value="jobName",required=false) String  jobName,

            @RequestParam(value="pageNum",required=false,defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize
    ){
        AjaxResult ajax = AjaxResult.success();
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        List<CrmJob> list = crmJobService.selectCrmJobByPreDay(jobName,userId);
        ajax.put("data",getDataTable(list));

        return ajax;
    }

    /**
     * 删除事件记录
     * 删除事件 同时删除记录
     */
    @GetMapping("/deleteCrmJobById")
    public AjaxResult remove(Long id)
    {
        return toAjax(crmJobService.deleteCrmJobAndLogById(id));
    }

    /**
     * 获取事件详细信息
     */
    @GetMapping(value = "/getCrmJobDetail")
    public AjaxResult getInfo(Long id)
    {
        if(id == null){
            return AjaxResult.error("请选择事件任务");
        }
        CrmJob crmJob=crmJobService.selectCrmJobById(id);
        String jobCustomer=crmJob.getJobCustomer();
        if(StringUtils.isNotEmpty(jobCustomer)){
            List<Long> ids = JSONArray.parseArray(jobCustomer, Long.class);
            List<CrmUser> list=crmCustomerService.selectCrmCustomerListByIds(ids);
            crmJob.setCustomerList(list);
        }
        return AjaxResult.success(crmJob);
    }
    public List<Long> stringToLongList(String strArr) {
        return Arrays.stream(strArr.split(","))
                .map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
    }

}
